package com.example.hybridmobiledemo.module.main.model.shop.classification.search;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;

import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.databinding.ActivitySearchBinding;
import com.example.hybridmobiledemo.databinding.ActivityShopSearchBinding;
import com.example.hybridmobiledemo.module.main.bean.NewsInfo;
import com.example.hybridmobiledemo.module.main.bean.SCNewsAllInfo;
import com.example.hybridmobiledemo.module.main.model.news.search.SearchActivity;
import com.example.hybridmobiledemo.module.main.model.shop.bean.Commodity;
import com.example.hybridmobiledemo.module.main.model.shop.bean.CommodityInfo;
import com.example.hybridmobiledemo.util.OkHttpUtils;
import com.example.hybridmobiledemo.view.ClaRViewAdapter;
import com.example.hybridmobiledemo.view.SeaPRvAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class ShopSearchActivity extends AppCompatActivity {
    private ActivityShopSearchBinding binding;
    private RecyclerView recyclerView;
    private List<Commodity> list = new ArrayList<>();
    private String search;
    private String UserID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        binding = ActivityShopSearchBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        SharedPreferences sp = getSharedPreferences("UserID", Context.MODE_PRIVATE);
        UserID = sp.getString("UserID", "ODky");
        Intent intent = getIntent();
        search = intent.getStringExtra("search");
        initView();
        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpUtils.builder()
                        .url("interface/Commodity/search")
                        .addParam("userId", UserID)
                        .addParam("commodityName", search)
                        .get()
                        .async(new OkHttpUtils.AsyncCallBack() {
                            @Override
                            public void onResponse(String json) {
                                Gson gson = new Gson();
                                CommodityInfo commodity = gson.fromJson(json, CommodityInfo.class);
                                List<CommodityInfo.DataBean> Temp = commodity.getData();
                                for (int i = 0; i < Temp.size(); i++) {
                                    String mId = String.valueOf(Temp.get(i).getCommodityTypeId());
                                    list.add(new Commodity(Temp.get(i).getId(),
                                            Temp.get(i).getCommodityName(),
                                            mId,
                                            Temp.get(i).getMoney(),
                                            Temp.get(i).getCostPrice(),
                                            Temp.get(i).getPic(),
                                            Temp.get(i).getVolume()));
                                }
                                //                                刷新UI控件
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        recyclerView.setLayoutManager(new LinearLayoutManager(ShopSearchActivity.this));
                                        recyclerView.setAdapter(new ClaRViewAdapter(list, ShopSearchActivity.this, getWindowManager(), UserID));
                                    }
                                });
                            }

                            @Override
                            public void onFailure(Exception e) {

                            }
                        });
            }
        }).start();
    }

    private void initView() {
        recyclerView = binding.rvShopSearch;
    }
}